home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 31 / Amiga Format CD31 (1998-09-02)(Future Publishing)(GB)(Track 1 of 2)[!][issue 1998-10].iso / -seriously_amiga- / hardware / transadf / source / write_disk.c < prev    next >
C/C++ Source or Header  |  1998-07-20  |  5KB  |  162 lines

  1. /* write-disk.c - Write a file straight onto a disk
  2. ** Copyright (C) 1997,1998 Karl J. Ots
  3. ** 
  4. ** This program is free software; you can redistribute it and/or modify
  5. ** it under the terms of the GNU General Public License as published by
  6. ** the Free Software Foundation; either version 2 of the License, or
  7. ** (at your option) any later version.
  8. ** 
  9. ** This program is distributed in the hope that it will be useful,
  10. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. ** GNU General Public License for more details.
  13. ** 
  14. ** You should have received a copy of the GNU General Public License
  15. ** along with this program; if not, write to the Free Software
  16. ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  17. */
  18.  
  19. #include <exec/types.h>
  20. #include <exec/memory.h>
  21. #include <dos/dos.h>
  22. #include <clib/exec_protos.h>
  23. #include <clib/dos_protos.h>
  24.  
  25. #include <string.h>
  26.  
  27. #include "write_disk.h"
  28. #include "main.h"
  29. #include "td.h"
  30. #include "mem_chunks.h"
  31. #include "util.h"
  32. #include "errors.h"
  33.  
  34.  
  35. #define WD_TBTRACKS 1                           /* Tracks in wd_TrackBuf */
  36. #define WD_TBSIZE   (WD_TBTRACKS * TRACK_SIZE)  /* Bytes in wd_TrackBuf  */
  37.  
  38. UBYTE *wd_TrackBuf;    /* Our input buffer - reads from file.    */
  39. UBYTE *wd_VTrackBuf;   /* Buffer for verifying data if required. */
  40.  
  41. /*
  42. ** Writes a file into a disk.
  43. ** Expects all fields of adfPkt to be set.
  44. */
  45. void writeDisk (struct ADF_Packet *adfPkt)
  46. {
  47.   BYTE TDError;
  48.   LONG DOSError, RWSize;
  49.   int i;
  50.   
  51.   
  52.   /* Output info header */
  53.   FPrintf (StdOut, "Writing from %s to TrackDisk Unit %ld (DF%ld:).\n", 
  54.                    adfPkt->ADFileName,
  55.                    adfPkt->diskUnit,
  56.                    adfPkt->diskUnit);
  57.   
  58.   FPrintf (StdOut, "Starting at track %ld, Ending at track %ld.\n",
  59.                    (adfPkt->startTrack>>1),
  60.                    (adfPkt->endTrack>>1));
  61.   
  62.   /* Allocate track buffers */
  63.   wd_TrackBuf = (UBYTE *) myAllocMem (WD_TBSIZE, MEMF_CLEAR);
  64.   if (!wd_TrackBuf)
  65.   {
  66.     /* Out of memory! */
  67.     FPrintf (StdErr, "%s: Out of memory.\n", ProgName);
  68.     cleanExit (RETURN_FAIL, ERROR_NO_FREE_STORE);
  69.   }
  70.   
  71.   if (adfPkt->verify)
  72.   {
  73.     wd_VTrackBuf = (UBYTE *) myAllocMem (WD_TBSIZE, MEMF_CLEAR);
  74.     if (!wd_VTrackBuf)
  75.     {
  76.       /* Out of memory! */
  77.       FPrintf (StdErr, "%s: Out of memory.\n", ProgName);
  78.       cleanExit (RETURN_FAIL, ERROR_NO_FREE_STORE);
  79.     }
  80.   }
  81.   
  82.   /* Start writing */
  83.   for (i = adfPkt->startTrack; i <= adfPkt->endTrack; i++)
  84.   {
  85.     /* Check for Control-C break */
  86.     if (CTRL_C)
  87.     {
  88.       FPutC (StdOut, '\n');
  89.       FPrintf (StdErr, "%s - %s\n", breakText, ProgName);
  90.       cleanExit (RETURN_WARN, NULL);
  91.     }
  92.     
  93.     /* Fill the buffer from file */
  94.     RWSize = Read (adfPkt->ADFile, wd_TrackBuf, WD_TBSIZE);
  95.     if (RWSize != WD_TBSIZE)
  96.     {
  97.       DOSError = IoErr();
  98.       
  99.       FPutC (StdOut, '\n');
  100.       FPrintf (StdErr, "%s: Error reading from %s - ", ProgName,
  101.                                                        adfPkt->ADFileName);
  102.       if (DOSError)
  103.         reportDOSError (DOSError);
  104.       else
  105.         FPuts (StdErr, "File to short.\n");
  106.       
  107.       cleanExit (RETURN_ERROR, DOSError);
  108.     }
  109.     
  110.     /* Update progress information */
  111.     FPuts (StdOut, "\rWriting   ");
  112.     FPUTS_TS (i, StdOut);
  113.     Flush (StdOut);
  114.     
  115.     /* Write the buffer to the disk */
  116.     TDError = writeTrack (wd_TrackBuf, WD_TBTRACKS, i, adfPkt->diskReq);
  117.     if (TDError)
  118.     {
  119.       FPutC (StdOut, '\n');
  120.       FPrintf (StdErr, "%s: Error writing to DF%ld: - ",ProgName,
  121.                                                         adfPkt->diskUnit);
  122.       reportTDError (TDError);
  123.       cleanExit (RETURN_ERROR, NULL);
  124.     }
  125.     
  126.     
  127.     if (adfPkt->verify)
  128.     {
  129.       /* Verify that the data was written correctly */
  130.       FPuts (StdOut, "\rVerifying ");
  131.       FPUTS_TS (i, StdOut);
  132.       Flush (StdOut);
  133.       
  134.       flushTrack (adfPkt->diskReq);
  135.       TDError = readTrack (wd_VTrackBuf, WD_TBTRACKS, i, adfPkt->diskReq);
  136.       if (TDError)
  137.       {
  138.         FPutC (StdOut, '\n');
  139.         FPrintf (StdErr, "%s: Error reading from DF%ld - ", ProgName,
  140.                                                             adfPkt->diskUnit);
  141.         reportTDError (TDError);
  142.         cleanExit (RETURN_ERROR, NULL);
  143.       }
  144.       
  145.       /* Check that the two buffers are identical */
  146.       if (memcmp (wd_TrackBuf, wd_VTrackBuf, WD_TBSIZE) != 0)
  147.       {
  148.         /* Verification error */
  149.         FPutC (StdOut, '\n');
  150.         FPrintf (StdErr, "%s: Verification Error on DF%ld:\n", ProgName,
  151.                                                                adfPkt->diskUnit);
  152.         cleanExit (RETURN_ERROR, NULL);
  153.       }
  154.     }
  155.   }
  156.   
  157.   /* Free buffers */
  158.   myFreeMem (wd_TrackBuf);
  159.   if (wd_VTrackBuf) 
  160.     myFreeMem (wd_VTrackBuf); 
  161. }
  162.